Reducing shader switches

Kanzi must switch shaders every time a rendered batch has a different shader compared to the previous batch. Because materials can have their own vertex and fragment shaders, in some cases your application has to switch shaders several times during the rendering of a scene.

In OpenGL vertex and fragment shaders are each compiled into a shader program and are sent to the GPU whenever it needs to be used. The GPU only stores one program at a time. At least one shader switch must be made for each material in a scene. If objects are presented in an order that requires excessive switching between shader programs, the rendering can slow down.

You can see the number of shader switches in the Kanzi Studio Preview Performance HUD, or in the Kanzi Engine API by calling getShaderSwitchCount(). See Troubleshooting the performance of your application.

Shader switching decreases the performance of your application, so keep shader switching to a minimum. To reduce shader switches:

Rendering nodes by material type

To render nodes by material type:

  1. In the Library press Alt and right-click Rendering > Object Sources and select Sorting Filter.
  2. In the Properties set:
  3. In the Library > Rendering > Render Passes create a Group Render Pass and inside it create:
  4. In the Library select the Draw Objects Render Pass that you created and in the Properties set the Object Source property to the filter that you created or to an object source which collects the filter.
  5. In the Project select the Scene node to which you want to apply the filter and in the Properties set the Render Pass property to the Group Render Pass whose child Draw Objects Render Pass uses as its object source the filter you created.

    Kanzi Studio renders the nodes collected by the filter.

See also

Using binary shaders

Optimizing fragment shaders

Loading resources in parallel

Shaders best practices

Troubleshooting the performance of your application

Best practices